<?php
namespace app\admin\service;

use library\tools\Node;
use think\Db;
use think\facade\Request;

/**
 * 系统用户管理服务
 * Class MemberService
 * @package app\admin\service
 */
class MemberService
{
    /**
     * 添加用户
     * @param string $loginname
     * @param string $password
     * @param string $type
     * @param string $source
     * @param string $account_type
	 * @param string $master
     * @return number
     */
    public static function member_add($loginname,$password,$type,$source,$account_type,$master)
    {
    	if(!self::member_check($loginname,$type)){   		
    		$data = [
	    		'loginname' => $loginname,
	    		'password' => md5($password),
	    		'type' => $type,
	    		'source' => $source,
	    		'account_type' => $account_type,
	    		'master' => $master ? $master : 0,
	    	]; 
	    	$result = Db::name('system_member')->insertGetId($data);      
	        return $result;
    	}else{
    		return -1;
    	}
    	
    }
    
    
    /**
     * 检测用户是否存在
     * @param string $loginname
     * @param string $password
     * @return boolean
     */
    public static function member_check($loginname,$type)
    {
    	$result = Db::name('system_member')->where(['loginname'=>$loginname,'type'=>$type])->count();
    	if($result){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    
    /**
     * 更改用户基本信息
     * @param int $id
     * @param array $data
     * @return boolean
     */
    public static function member_edit($id,$data)
    {
    	$result = Db::name('system_member')->where(['id'=>$id])->update($data);
    	if($result){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    
    /**
     * 根据公司表ID或者个人表ID获取member表ID
     * @param string $id
     * @param string $type  1 公司   2个人
     * @return number
     */
    public static function member_id($id,$type)
    {
    	if($type == 1){
    		$member_id = Db::name('system_company')->where(['id'=>$id])->value('mid');
    	}
    	
    	if($member_id){
    		return $member_id;
    	}else{
    		return 0;
    	}
    }
    
	
	/**
     * 根据个人身份证获取member表ID和person表ID
     * @param string $id_number
     * @return array
     */
    public static function member_id_person($id_number)
    {  	
    	$member_id = Db::name('system_member')->where(['loginname'=>$id_number])->value('id');	
    	if($member_id){
    		$person_id = Db::name('system_person')->where(['mid'=>$member_id])->value('id');	
    		return ['mid'=>$member_id,'person_id'=>$person_id];
    	}else{
    		return ['mid'=>0,'person_id'=>0];
    	}
    }
	
	
	/**
     * 增员
     * @param string $name
	 * @param string $id_number
	 * @param string $mobile
	 * @param string $company_id
	 * @param string $csr_id
	 * @param string $bank_account
	 * @param string $bank
	 * @param string $bank_number
	 * @param int 	 $archives
     * @return array
     */
    public static function member_person_add($name,$id_number,$mobile,$company_id,$csr_id,$bank_account,$bank,$bank_number,$archives)
	{
		$re = self::member_add($id_number,'123456',2,1,1,0);
		if($re == -1){
			$mid = Db::name('system_member')->where(['loginname'=>$id_number])->value('id');
			$person = Db::name('system_person')->where(['mid'=>$mid])->find();
			if($person){
				$data = [
					'name' => $name,
					'id_number' => $id_number,
					'mobile' => $mobile,
					'company_id' => $company_id,
					'csr_id' => $csr_id,
					'status' => 1,
					'bank_account' => $bank_account,
					'bank' => $bank,
					'bank_number' => $bank_number,
					'archives' => $archives
				];
				Db::name('system_person')->where(['mid'=>$mid])->update($data);
				$person_id =  Db::name('system_person')->where(['mid'=>$mid])->value('id');	
			}else{
				$data = [
					'mid' => $mid,
					'name' => $name,
					'id_number' => $id_number,
					'mobile' => $mobile,
					'company_id' => $company_id,
					'code' => number_rule_max(8),
					'csr_id' => $csr_id,
					'bank_account' => $bank_account,
					'bank' => $bank,
					'bank_number' => $bank_number,
					'archives' => $archives
				];
				$person_id = Db::name('system_person')->insertGetId($data); 
				if($person_id){
					save_number_rule_max(8);
				}
			}
					
		}else{
			$data = [
				'mid' => $re,
				'name' => $name,
				'id_number' => $id_number,
				'mobile' => $mobile,
				'company_id' => $company_id,
				'code' => number_rule_max(8),
				'csr_id' => $csr_id,
				'bank_account' => $bank_account,
				'bank' => $bank,
				'bank_number' => $bank_number,
				'archives' => $archives
			];
			$person_id = Db::name('system_person')->insertGetId($data); 
			if($person_id){
				save_number_rule_max(8);
			}
		}
		return $person_id;
	}


	/**
	 * 导出员工
     * @param array $id
     * @return array
     */
    public static function export_staff($id)
    {
    	$filename = '员工列表';
    	$titles = array(
    		array('code','员工编号'),
    		array('name','姓名'),
    		array('id_number','身份证号'),
            array('mobile','电话号码'),                       
            array('company_name','公司名称'),       
            array('status','状态'),
            array('csr_cn','所属客服'),
        );
    	$list = Db::name('system_person')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['name'] = $v['name'];
				$arr[$k]['id_number'] = ' '.$v['id_number'];
				$arr[$k]['mobile']	= $v['mobile']; 	
				$arr[$k]['company_name'] = $companys[$v['company_id']];					
				$arr[$k]['status'] = ['1'=>'在职','2'=>'解约','3'=>'离职'][$v['status']];	
				$arr[$k]['csr_cn'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }



	/**
	 * 导出公司会员
     * @param array $id
     * @return array
     */
    public static function export_member($id)
    {
    	$filename = '公司会员列表';
    	$titles = array(
    		array('unique','公司编号'),
    		array('company_name','公司名称'),
    		array('account_type','消费类型'),
            array('point','服务点数'),                       
            array('login_num','登录次数'),       
            array('login_at','最近登录时间'),
            array('status','状态'),
            array('create_at','创建时间')
        );
    	$list = Db::name('system_member')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			foreach($list as $k=>$v){
				$company = Db::name('system_company')->where(['mid'=>$v['id']])->field('unique,name,point')->find();
	    		$arr[$k]['unique'] = $company['unique'];
				$arr[$k]['company_name'] = $company['name'];
				$arr[$k]['account_type'] = ['1'=>'主账号','2'=>'子账号'][$v['account_type']];	
				$arr[$k]['point'] = $company['point'];
				$arr[$k]['login_num']	= $v['login_num']; 	
				$arr[$k]['login_at'] = $v['login_at'] ?  date('Y-m-d H:i',$v['login_at']) : '';	    						
				$arr[$k]['status'] = ['0'=>'禁用','1'=>'正常'][$v['status']];	
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    /**
	 * 导出员工会员
     * @param array $id
     * @return array
     */
    public static function export_person($id)
    {
    	$filename = '员工会员列表';
    	$titles = array(
    		array('loginname','身份证号'),
    		array('mobile','手机号码'),
    		array('source','注册来源'),
    		array('login_num','登录次数'),
    		array('login_at','最近登录时间'),
            array('login_source','登录平台'),                                   
            array('status','状态'),
            array('create_at','创建时间')
        );
    	$list = Db::name('system_member')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			foreach($list as $k=>$v){
	    		$arr[$k]['loginname'] = ' '.$v['loginname'];
				$arr[$k]['mobile'] = $v['mobile'];
				$arr[$k]['source'] = ['1'=>'系统添加','2'=>'前台注册'][$v['source']];
				$arr[$k]['login_num']	= $v['login_num']; 	
				$arr[$k]['login_at'] = $v['login_at'] ?  date('Y-m-d H:i',$v['login_at']) : '';		
				$arr[$k]['login_source'] = ['1'=>'PC网站','2'=>'移动H5'][$v['login_source']];				    						
				$arr[$k]['status'] = ['0'=>'禁用','1'=>'正常'][$v['status']];	
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
}
